package com.etrans.web.gps.business.report.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.timer.LocalCache;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;

/**
 *报警综合分析Service
 * @author zhangw
 *
 */
@Service
public class DataMonitorService extends BaseService{

	private final static String TABLE = "data_quality_analyse";
	
	/**
	 * 根据条件查询数据总数
	 * @param filter
	 * @return
	 */
	public int getTableDataCount(BasicDBObject query) {
		return MGTemplete.findCount(DBNAME, TABLE, query).intValue();
	}

	/**
	 * 根据条件进行分页查询
	 * @param filter
	 * @param monthStr
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<Document> getTableDataList(BasicDBObject query, int page, int size) {
		BasicDBObject sort = new BasicDBObject("$alarm_time",-1);
		FindIterable<Document> docs = MGTemplete.getCollection(DBNAME, TABLE).find(query).sort(sort).skip(page).limit(size);
		List<Document> list = new ArrayList<Document>();
		Map<Integer, String> entMap = LocalCache.entMap;
		Map<Integer, String> platMap = LocalCache.platMap;
		for (Document doc : docs) {
			doc.put("enterprise_name", entMap.get(doc.getInteger("enterprise_id",0)));
			doc.put("platform_name", platMap.get(doc.getInteger("platform_id",0)));
			doc.put("alarm_src", 3);
            list.add(doc);
        }
		return list;
	}

	/**
	 * 查询图形数据
	 * @param query
	 * @param list 
	 * @return
	 */
	public Map<String, Object> getLineChartData(BasicDBObject query, List<String> days) {
		List<Bson> pipeline = new ArrayList<>();
		pipeline.add(new BasicDBObject("$match", query));
		//分组
		DBObject groupFields = new BasicDBObject("exception_type", "$exception_type");
        groupFields.put("exception_time", "$exception_time");
        DBObject group = new BasicDBObject("_id", groupFields);
        group.put("count", new BasicDBObject("$sum", 1));
        pipeline.add(new BasicDBObject("$group", group));
        Bson sort = new BasicDBObject("$sort",  new BasicDBObject("_id.exception_time", 1));
        pipeline.add(sort);
		AggregateIterable<Document> docs = MGTemplete.getCollection(DBNAME, TABLE).aggregate(pipeline);
		List<Integer>  y1Data =new ArrayList<>();
		List<Integer>  y2Data =new ArrayList<>();
		List<Integer>  y3Data =new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Integer> map = new HashMap<>();
		for (Document doc : docs) {
			Document id = (Document) doc.get("_id");
			String key = sdf.format(id.getDate("exception_time")) + "-" + id.get("exception_type");
			if (map.containsKey(key)) {
				map.replace(key, doc.getInteger("count", 0) + map.get(key));
			}else {
				map.put(key, doc.getInteger("count", 0));
			}
		}
		for (String day : days) {
			y1Data.add(map.getOrDefault(day + "-" + 1, 0));
			y2Data.add(map.getOrDefault(day + "-" + 2, 0));
			y3Data.add(map.getOrDefault(day + "-" + 3, 0));
		}
		Map<String,Object> resultMap =new HashMap<String, Object>();
		List<Object>  SeriesData =new ArrayList<Object>();
		SeriesData.add(y1Data);
		SeriesData.add(y2Data);
		SeriesData.add(y3Data);
		resultMap.put("xAxisData", days);
		resultMap.put("series", SeriesData);
		return resultMap;
	}
	
}
